package com.example.myblog.controller;


import com.example.myblog.entity.Comment;
import com.example.myblog.exception.BaseException;
import com.example.myblog.result.ResponseBean;
import com.example.myblog.service.CommentService;
import com.example.myblog.vo.CommentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 评论管理 前端控制器
 * </p>
 *
 * @author 叶兴
 * @since 2022-09-29
 */
@RestController
@RequestMapping("/comment")
public class CommentController {

    CommentService commentService;

    @Autowired
    public void setCommentService(CommentService commentService) {
        this.commentService = commentService;
    }

    /**
     * 新增一条对文章的评论
     * @param commentVO
     * @return
     * @throws BaseException
     */
    @PostMapping("/blog")
    public ResponseBean commentBlog(CommentVO commentVO) throws BaseException {
        commentService.insertOneCommentForBlog(commentVO);
        return ResponseBean.success();
    }

    /**
     *  新增一条对用户的评论
     * @param commentVO
     * @return
     * @throws BaseException
     */
    @PostMapping("/user")
    public ResponseBean commentUser(CommentVO commentVO) throws BaseException {
        commentService.insertOneCommentForUser(commentVO);
        return ResponseBean.success();
    }

    /**
     * 获取当前文章下的所有评论
     * @param id
     * @return
     * @throws BaseException
     */
    @GetMapping("/{id}")
    public ResponseBean getAllComments(@PathVariable String id) throws BaseException {
        List<Comment> list = commentService.getAllComments(id);
        return ResponseBean.success(list);
    }
}
